define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope,$rootScope, T_address_add_fp, GoPage, $stateParams, GoDialog, $ionicModal) {
		$scope.scope = new Object();
		$scope.datas = new Object();
		
		//初始化页面
		$scope.init_data = function() {
			T_address_add_fp.invoice_detail(function(data) {
				if(data!=null){
					$scope.datas = data;
					$scope.datas.isAgree = true;
				}
			})
		}
		$scope.init_data(); //必须在定义之后
		
		//阅读“增值税专用发票协议”
		$scope.handleIsAgree = function(){
			if($scope.datas.status_code=='1') return;//审核通过后，勾就不能去掉
			$scope.datas.isAgree = !$scope.datas.isAgree;
		}
		
		//清空输入
		$scope.clearInput = function(type){
			if(type==1)  $scope.datas.company_name = "";
			if(type==2)  $scope.datas.tax_no = "";
			if(type==3)  $scope.datas.address = "";
			if(type==4)  $scope.datas.phone = "";
			if(type==5)  $scope.datas.bank_name = "";
			if(type==6)  $scope.datas.bank_no = "";
		}
		
		//点击感叹号提示信息
		$scope.showTip = function(){
			GoDialog.alert("提示","审核通过后，税号不能修改");
		}
		
		//提交
		$scope.onshow_address = function() {
			if(!$scope.datas.company_name) {
				GoDialog.toast("请输入公司名称");
				return
			} else if(!$scope.datas.tax_no) {
				GoDialog.toast("请输入税号");
				return
			} else if(!$scope.datas.address) {
				GoDialog.toast("请输入注册地址");
				return
			} else if(!$scope.datas.phone) {
				GoDialog.toast("请输入电话号码");
				return
			} else if(!$scope.datas.bank_name) {
				GoDialog.toast("请输入开户银行");
				return
			} else if(!$scope.datas.bank_no) {
				GoDialog.toast("请输入开户账号");
				return
			}else if(!$scope.datas.isAgree) {
				GoDialog.alert("提示","请增值税专用发票协议");
				return
			}
			T_address_add_fp.invoice_edit($scope.datas.company_name, $scope.datas.tax_no, $scope.datas.address, $scope.datas.phone, $scope.datas.bank_name, $scope.datas.bank_no, function(data) {
				if(data.success == true) {
					GoPage.go_back(-1); //返回上一页
					//注册了全局广播
					$rootScope.$broadcast('editSettingEvent');
				} else{
					GoDialog.alert("提示",data.msg);
				}
			})
		}
		
		/***********显示《增值税专用发票协议》**********/
		$ionicModal.fromTemplateUrl("templates/modal/invoice_rule.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_invoice = modal;
		});
		//打开
		$scope.openModal_invoice = function() {
			$scope.modal_invoice.show();
		}
		//隐藏
		$scope.closeModal_invoice = function() {
			$scope.modal_invoice.hide();
		}
		/***********显示《增值税专用发票协议》**********/
		
		
	}

	controller.$inject = ['$scope','$rootScope', 'T_address_add_fp', 'GoPage', '$stateParams', 'GoDialog','$ionicModal'];

	return app.registerController("address_add_fpctrl",controller);
});